package com.burgeon.backend.Rest.User;
/**
 * 获取零售信息
 */

import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.text.SimpleDateFormat;
import java.util.*;

public class GetRetailData extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public GetRetailData(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     * @return
     */

    public TreeMap<String, Object> getData() throws Exception {
        String url = "http://"+ip+"/servlets/binserv/Rest";

        String str = RESULT(Data(),url);

        String code = new JSONArray(str).getJSONObject(0).getString("code");
        int count = new JSONArray(str).getJSONObject(0).getInt("count");

        if (!"0".equals(code)){
           throw new Exception("视图错误");
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");

        TreeMap<String,Object> mdate = new TreeMap<String,Object>();

        TreeMap<String,Integer> xs = new TreeMap<String,Integer>();
        TreeMap<String,Integer> xx = new TreeMap<String,Integer>();
        List datec = new ArrayList();
        for (int i=0;i<7;i++){
            Calendar c = Calendar.getInstance();
            c.add(Calendar.DATE, -i);
            Date date = c.getTime();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            String sTime = sdf.format(date);
            datec.add(sTime);
            xs.put(sTime,0);
            xx.put(sTime,0);
        }
        mdate.put("date",datec);

        List xsData = new ArrayList();
        List xxData = new ArrayList();
        for (int i=0;i<jsonArray.length();i++){
            if ("是".equals(jsonArray.getJSONArray(i).getString(1))){//线上
                xs.put(jsonArray.getJSONArray(i).getString(0),xs.get(jsonArray.getJSONArray(i).getString(0))+1);
            }else {//线下
                xx.put(jsonArray.getJSONArray(i).getString(0),xx.get(jsonArray.getJSONArray(i).getString(0))+1);
            }
           // mdate.put(jsonArray.getJSONArray(i).getString(0),(int)mdate.get(jsonArray.getJSONArray(i).getString(0))+1);
        }

        for (Object o : datec) {
            xsData.add(xs.get(o));
            xxData.add(xx.get(o));
        }
        mdate.put("xsData",xsData);
        mdate.put("xxData",xxData);

        mdate.put("yesterday",xs.get(datec.get(1))+xx.get(datec.get(1)));
        mdate.put("today",xs.get(datec.get(0))+xx.get(datec.get(0)));
        mdate.put("week",count);
        return mdate;
    }

    private MultiValueMap Data() {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段
        //IS_YCRETAIL = N   为 线下    =Y  就是 线上
        columns.add("BILLDATE");
        columns.add("IS_YCRETAIL");

        params.put("columns", columns);
        params.put("table", "M_RETAIL");//表名

        params.put("count","true");

        HashMap orderby = new HashMap();

        orderby.put("column","OPENCARDDATE");
        orderby.put("asc","false");

        Calendar c = Calendar.getInstance();
        c.add(Calendar.DATE, -6);
        Date date = c.getTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String sTime = sdf.format(date);
        Date date1 = new Date();
        String oTime = sdf.format(date1);

        /*HashMap paramss = new HashMap();
        String ac = "'"+ sTime+"~"+oTime +"'";
        paramss.put("column", "BILLDATE");
        paramss.put("condition", ac);
        params.put("params", paramss);*/

        HashMap paramss = new HashMap();
        HashMap expr1 = new HashMap();
        String ac = "'"+ sTime+"~"+oTime +"'";
        expr1.put("column", "BILLDATE");
        expr1.put("condition", ac);
        HashMap expr2 = new HashMap();
        expr2.put("column", "'ISACTIVE'");
        expr2.put("condition", "'Y'");

        paramss.put("combine", "and");
        paramss.put("expr1", expr1);
        paramss.put("expr2", expr2);

        params.put("params", paramss);

        params.put("range",50000);

        return DATA("Query", params,account,password);
    }

    public static void main(String[] args) {
        GetRetailData getRetailData = new GetRetailData("59.110.28.94:90", "nea@burgeon.com.cn", "burgeon55321");
        try {
            System.err.println(getRetailData.getData());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
